<?php

class Ingredients extends CI_Model {
    function __construct(){
        parent::__construct();
    }
    
    function getAll(){
        $this->db->order_by("ingredient_title", "asc");
        return $this->db->get('ingredients');
    }
    
    function insert($title){
        $this->db->set("ingredient_title",$title);
        $this->db->insert('ingredients');
    }
    
    function getPartial($query){
        $this->db->order_by("ingredient_title", "asc");
        $this->db->like("ingredient_title",$query);
        return $this->db->get("ingredients");
    }
    
    function getPartialBasetype($query, $basetype){
        $this->db->from("ingredients AS I");
        
        if (trim($query) != ""){
            $this->db->like("I.ingredient_title", $query);
        }
        
        if (trim($basetype) != -1){
            $this->db->join("basetype_ingredients AS BI","BI.ingredient_id = I.ingredient_id");
            $this->db->where("BI.basetype_id", $basetype);
        }
        
        $this->db->order_by("I.ingredient_title", "asc");
        return $this->db->get();
    }
    
    function getIngredient($id){
        $this->db->where("ingredient_id",$id);
        $result = $this->db->get("ingredients");
        return $result->row();
    }
    
    function getBasetypes($id){
        $this->db->from("basetype_ingredients as BI");
        $this->db->join("basetypes as B", "BI.basetype_id = B.basetype_id");
        $this->db->where("BI.ingredient_id",$id);
        return $this->db->get();
    }
    
    function getByTitle($title){
        $this->db->where('ingredient_title', $title);
        $ingredient = $this->db->get("ingredients");
        if ($ingredient->num_rows() > 0){
            return $ingredient->row();
        }
        else{
            return null;
        }
    }
    
    function create($ingredient_title,$basetypes){
        $this->db->trans_start();
        
        $data["ingredient_title"] = $ingredient_title;
        $query = $this->db->insert('ingredients', $data);
        $ingredient_id = $this->db->insert_id();

        unset($data);
        $data['ingredient_id'] = $ingredient_id;

        foreach($basetypes as $basetype){
            $data['basetype_id'] = $basetype;
            $query = $query && $this->db->insert('basetype_ingredients',$data);
        }
        
        $this->db->trans_complete();
        
        if ($query){
            return true;
        }
        else{
            return false;
        }
        
    }
    
    function update($ingredient_id, $ingredient_title,$basetypes){
        
        $this->db->trans_start();
        
        //Remove all ingredients and categories
        $this->db->where('ingredient_id', $ingredient_id);
        $this->db->delete('basetype_ingredients');
        
        $data["ingredient_title"] = $ingredient_title;
        $this->db->where('ingredient_id', $ingredient_id);
        $query = $this->db->update('ingredients', $data);

        unset($data);
        $data['ingredient_id'] = $ingredient_id;

        foreach($basetypes as $basetype){
            $data['basetype_id'] = $basetype;
            $query = $query && $this->db->insert('basetype_ingredients',$data);
        }
        
        $this->db->trans_complete();
        
        if ($query){
            return true;
        }
        else{
            return false;
        }
    }
    
    function deleteIngredient($ingredient_id){
        $this->db->trans_start();
        
        $this->db->where("ingredient_id",$ingredient_id);
        $this->db->delete("meal_ingredients");
        
        
        $this->db->where("ingredient_id",$ingredient_id);
        $this->db->delete("basetype_ingredients");
        
        
        $this->db->where("ingredient_id",$ingredient_id);
        $this->db->delete("ingredients");
                
        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
}
